<?php

/**
 * 作者：lucas
 * 链接：
 * 邮箱：dissipator_520@qq.com
 */

/**
 * NodeAction
 *
 * @author lucas
 */
class FhAction extends BaseAction {

    public function index() {
        $this->display();
    }

    public function upfhqr() {
         $this-> display();
    }

     public function getEChartsdata($t='line') {

        $map=$this->condition;
        $Mode = D('FhView');
        $data = $Mode->getECdata();
//<<<<<<< HEAD
        $option=array();
        $d=array();
        $n=array();
        if($data){
             $option['yAxis'][]=array('type'=>'value');
                foreach ($data as $key => $value) {
                    $n[]=$value['n'];
                    $d[]=(int)$value['d'];
                }
             $option['xAxis'][]=array('data'=>$n);
             $option['series'][]=array('data'=>$d,'name'=>"2012Y",'type'=>$t);

        }
        // return response($output,'json');
        // p($option);
         $this->ajaxReturn($option);
//=======
        Log::write($Mode->getLastSql());
        $this->getECharts($data,'line');
        // $this->response($data,'json');
//>>>>>>> 421d9175a2767bf3e4fa121f5eb5a1e98b092ce8
    }

    public function getData() {

        $map=$this->condition;
        $Mode = M('Fhqr');
        // p($Mode);
        $data = $Mode->where($this->condition)->page($this->page, $this->rows)->order($this->order)->select();
        Log::write($Mode->getLastSql());
        // foreach ($map as $key => $value) {
        //     Log::write($key.' '.$value[0].$value[1]);
        //     // p($value);
        // }      
        $this->returnGridData($data, $Mode->where($this->condition)->count());
    }

    public function toAdd() {

        $Model = M("Fhqr");
            // $fields=$Model->getDbFields();
            $fields=array('ID','times','PO','PRODUCT','PACKAGE','LOTNO','QTY','HK','USA','UK','TWN','COMMENTS','FHDATE','MUNO','BZ');
            if ($fields) {
                foreach ($fields as $key ) {
                    switch ($key)
                    {
                    case 'ID':
                        $output .= '';
                        break; 
                    default:
                        $output .= '<tr><td>' .$key .'：</td><td><input name="'.$key.'" required="required" type="text" value="" style="width: 150px;"/></td></tr>';
                    }
                }
                $this->assign('output', $output);
            }
        $this->display('add');
    }

    public function doSave() {

        $Model = M("fhqr");
        $data=$_POST;
        // p($data);
        if ($data['ID']){
            $Model -> data($data) -> where('ID=%d',$data['ID']) -> save();
            $result = true;
        }else{ 
                if (!$Model->create()) {
                    $this->returnStatus(false, $Model->getError());
                } else {
                    $Model->startTrans();
                    $result = false;
                    if (empty($Model->ID)) {
                        $result = $Model->data($data)->add();
                        // p($data);
                    }
        }}       
            $Model->commit();
            $this->returnStatus();
    }


    public function doDelete() {

        $ids = $_POST['ids'];

        $array = explode(',', $ids);
        // p($array);
        if ($array && count($array) > 0) {
            $Model = M("fhqr");
            foreach ($array as $id) {
                $Model->where('ID = %d', $id)->delete();
            }
        }

        $this->returnStatus();
    }

    public function toUpdate() {

        $ID = $_GET['ID'];

        $fields=array('ID','times','PO','PRODUCT','PACKAGE','LOTNO','QTY','HK','USA','UK','TWN','COMMENTS','FHDATE','MUNO','BZ');
        if ($ID) {
            $Model = M('fhqr');
            $data = $Model->where('ID = %d', $ID)->field($fields)->find();
            // p($data);
            if ($data) {
                foreach ($data as $key => $value) {
                    switch ($key)
                    {
                    case 'ID':
                        $output .= '<input name="ID" required="required" type="hidden" value="' .$value. '" style="width: 150px;"/>';
                        break; 
                    case 'times':
                        $output .= '';
                        break;  
                    default:
                        $output .= '<tr><td>' .$key .'：</td><td><input name="'.$key.'" required="required" type="text" value="' .$value. '" style="width: 150px;"/></td></tr>';
                    }
                }
                $this->assign('output', $output);
                // $this->assign('data', $data);
            }
        }
        $this->display('add');
    }

    public function upexcel(){

            if (! empty ( $_FILES ['file_stu'] ['name'] )){
                    $tmp_file = $_FILES ['file_stu'] ['tmp_name'];
                    $file_types = explode ( ".", $_FILES ['file_stu'] ['name'] );
                    $file_type = $file_types [count ( $file_types ) - 1];

                     /*判别是不是.xls文件，判别是不是excel文件*/
                     if (strtolower ( $file_type ) != "xlsx"){
                       $this->error ( '不是Excel文件，重新上传' );
                     }

                    /*设置上传路径*/
                     // $savePath = APP_PATH .'Runtime/Data/Excel/';
                    $savePath = APP_PATH.'Runtime/Data/Excel/';
                    /*以时间来命名上传的文件*/
                     $str = 'tmp';// date ( 'Ymdhis' ); 
                    //要导入的xls文件，位于根目录下的Public文件夹
                     $file_name = $str . "." . $file_type;
                    // echo   $savePath . $file_name;
                     /*是否上传成功*/
                     if (! copy ( $tmp_file, $savePath . $file_name )){
                         $this->error ( '上传失败' );
                      }
            }
                $arr = array();  //声明数组
                //导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入

                import("@.ORG.Util.PHPExcel");

                //要导入的xls文件，位于根目录下的Public文件夹
                $filename =  $savePath . $file_name;
                //创建PHPExcel对象，注意，不能少了\

                $PHPExcel=new PHPExcel();
                //如果excel文件后缀名为.xls，导入这个类
                // import("Org.Util.PHPExcel.Reader.Excel5");
                // $PHPReader=new PHPExcel_Reader_Excel5();
                //如果excel文件后缀名为.xlsx，导入这下类
                import("Org.Util.PHPExcel.Reader.Excel2007");
                $PHPReader=new \PHPExcel_Reader_Excel2007();

                //载入文件
                $PHPExcel=$PHPReader->load($filename);
                // p($PHPExcel);
                //获取表中的第一个工作表，如果要获取第二个，把0改为1，依次类推
                $currentSheet=$PHPExcel->getSheet(0);
                //获取总列数
                $allColumn='P';
                //获取总行数
                $allRow=$currentSheet->getHighestRow();
                // 循环获取表中的数据，$currentRow表示当前行，从哪行开始读取数据，索引值从0开始
                for($currentRow=2;$currentRow<=$allRow;$currentRow++){
                    //从哪列开始，A表示第一列
                    for($currentColumn='B';$currentColumn<$allColumn;$currentColumn++){
                        // 数据坐标
                        $address=$currentColumn.$currentRow;
                        // 读取到的数据，保存到数组$arr中
                        $arr[$currentRow][$currentColumn]=$currentSheet->getCell($address)->getValue();
                    }
                }

                // p($arr);
            $data=array();

            //获取信息状态
             $fields =array(
                'B'=>'times',
                'C'=>'PO',
                'D'=>'PRODUCT',
                'E'=>'PACKAGE',
                'F'=>'LOTNO',
                'G'=>'QTY',
                'H'=>'HK',
                'I'=>'USA',
                'J'=>'UK',
                'K'=>'TWN',
                'L'=>'COMMENTS',    
                'M'=>'FHDATE',
                'N'=>'MUNO',
                'O'=>'BZ'
                );
            $j=0;
            // p($arr);
            // $map['times']= array('eq',$arr[2]['B']);
            $map['FHDATE']=array('eq',excelTime($arr[2]['M']));

            $count= M('fhqr')->where($map)->count();

            if ($count>0){

               if(M('fhqr')->where($map)->delete())echo '删除完毕';
            }
            foreach($arr as $i=>$k){
                    for($currentColumn='B';$currentColumn<$allColumn;$currentColumn++){
                         if ($k[$currentColumn])$data[$fields[$currentColumn]] =$k[$currentColumn];
                   }
                   $data['FHDATE']=$map['FHDATE'][1];
                   // p($data);
                   if (M('fhqr')->add($data))$j++;
            }
            // if($j>0) echo '导入数据 '.$j.' 条'; 
            if($j>0) $this -> success('导入数据 '.$j.' 条');     
    }

}

?>
